package com.apache.website.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.api.vo.Token;
import com.apache.database.constant.Validator;
import com.apache.database.model.Page;
import com.apache.tools.DateUtils;
import com.apache.uct.common.BaseAction;
import com.apache.website.entity.Channel;
import com.apache.website.entity.Manuscript;
import com.apache.website.entity.ManuscriptVo;
import com.apache.website.manager.ChannelManager;
import com.apache.website.manager.ManuscriptManager;

@Controller
@RequestMapping("wcm/manuscript/")
public class ManuscriptAction extends BaseAction<Manuscript> {

	@Autowired
	private ManuscriptManager manuscriptManager;
	@Autowired
	private ChannelManager channelManager;

	protected BaseManager<Manuscript> getBaseManager() {
		return manuscriptManager;
	}

	protected String getPrefix() {
		return "website/manuscript-";
	}

	/**
	 * 功能说明：跳转到列表查询页面
	 */
	@RequestMapping("list.action")
	public ModelAndView list(HttpServletRequest request) {
		ModelAndView view = null;
		String toPage = request.getParameter("toPage");
		if (Validator.isNotNull(toPage)) {
			view = new ModelAndView("website/artice-list");
		} else {
			view = new ModelAndView(LIST);
		}
		view.addObject("channelId", request.getParameter("channelId"));
		return view;
	}

	@RequestMapping("list!data.action")
	@ResponseBody
	protected Object data(HttpServletRequest request, String page, String rows) {
		ParamsVo<Manuscript> vo = new ParamsVo<Manuscript>();
		Manuscript lock = new Manuscript();
		if (Validator.isNotNull(request.getParameter("channelId"))) {
			lock.setChannelId(request.getParameter("channelId"));
		}
		if (Validator.isNotNull(request.getParameter("title"))) {
			lock.setTitle(request.getParameter("title"));
		}
		if (Validator.isNotNull(request.getParameter("status"))) {
			lock.setStatus(request.getParameter("status"));
		}
		vo.setParams("pageIndex", page);
		vo.setParams("pageSize", rows);
		vo.setObj(lock);
		Page pageList = manuscriptManager.getPageInfo(vo);
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("total", pageList.getCount());
		mapData.put("rows", pageList.getPageObjects());
		return mapData;
	}

	/**
	 * 功能说明：跳转到新增页面
	 */
	@Token(save = true)
	@RequestMapping(value = "add.action", method = RequestMethod.GET)
	protected ModelAndView add(HttpServletRequest request) {
		ModelAndView view = new ModelAndView(EDIT);
		ParamsVo<Manuscript> vo = new ParamsVo<Manuscript>();
		String manuscriptId = request.getParameter("manuscriptId");
		String channelId = request.getParameter("channelId");
		ManuscriptVo org = null;
		if (Validator.isNotNull(manuscriptId)) {
			vo.setInfoId(manuscriptId);
			vo.setMethodKey("AllById");
			org = (ManuscriptVo) getBaseManager().getInfoById(vo);
			ParamsVo cvo = new ParamsVo();
			cvo.setInfoId(org.getChannelId());
			Channel channel = (Channel) channelManager.getInfoById(cvo);
			view.addObject("channelName", channel.getChannelName());
		} else {
			org = new ManuscriptVo();
			org.setChannelId(channelId);
			ParamsVo cvo = new ParamsVo();
			cvo.setInfoId(channelId);
			Channel channel = (Channel) channelManager.getInfoById(cvo);
			org.setChannelCode(channel.getCodeName());
			view.addObject("channelName", channel.getChannelName());
		}
		view.addObject("item", org);
		return view;
	}

	@Token(remove = true)
	@RequestMapping(value = "/save.action", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg save(HttpServletRequest request, HttpServletResponse response, @ModelAttribute Manuscript item) {
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");
		ParamsVo<Manuscript> vo = getParamsVo(request);
		if (Validator.isNull(item.getChannelId())) {
			rmsg = getResultMsg(request, "所属栏目不能为空！");
			return rmsg;
		}
		if (Validator.isNotNull(item.getManuscriptId())) {//对稿件信息修改
			if (Validator.isNull(item.getUrl()) && "1".equals(item.getType())) {
				item.setUrl(item.getChannelCode() + "/" + DateUtils.format(new Date(), "yyyyMMdd") + "/"
						+ DateUtils.format(new Date(), "HHmmssSSS") + ".shtml");
			}
			vo.setObj(item);
			vo.setParams("content", request.getParameter("content"));
			boolean mark = getBaseManager().editInfo(vo);
			if (!mark) {
				rmsg = getResultMsg(request, "稿件信息修改失败！");
			}
		} else {//添加稿件信息
			item.setCreatorId(this.getSessionUser(request).getUserEname());
			item.setCreatedTime(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			if ("1".equals(item.getType())) {
				item.setUrl(item.getChannelCode() + "/" + DateUtils.format(new Date(), "yyyyMMdd") + "/"
						+ DateUtils.format(new Date(), "HHmmssSSS") + ".shtml");
				item.setStatus("0");
			} else {
				item.setStatus("2");
			}
			item.setViewNumber(0l);
			item.setSeqNum(65535l);
			item.setIsQuoted("T");
			item.setIsPageBreak("F");
			vo.setObj(item);
			vo.setParams("content", request.getParameter("content"));
			String id = getBaseManager().saveInfo(vo);
			if (Validator.isNull(id)) {
				rmsg = getResultMsg(request, "稿件信息添加失败！");
			}
		}
		return rmsg;
	}

	/**
	 * 修改状态
	 * @param request
	 * @param manuscriptId
	 * @return
	 */
	@RequestMapping(value = "/publish.action", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg updateStatus(HttpServletRequest request, String manuscriptId) {
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");
		ParamsVo<Manuscript> vo = getParamsVo(request);
		Manuscript mt = new Manuscript();
		mt.setManuscriptId(manuscriptId);
		vo.setInfoId(manuscriptId);
		Manuscript arite = (Manuscript) manuscriptManager.getInfoById(vo);
		if ("publish".equals(request.getParameter("type"))) {
			if (Validator.isEmpty(arite.getPublishedTime())) {
				mt.setPublishedTime(System.currentTimeMillis());
			}
			mt.setStatus("1");
		} else {
			mt.setStatus("3");
		}
		vo.setMethodKey("ByIdForStatus");
		vo.setObj(mt);
		boolean mark = manuscriptManager.editInfo(vo);
		if (!mark) {
			rmsg = new ResultMsg("T", "操作失败！");
		}
		return rmsg;
	}
}
